package com.txzt.gaea.business.device.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.business.device.domain.model.DeviceInfo;
import com.txzt.gaea.business.device.domain.model.DevicePurchase;
import com.txzt.gaea.business.device.service.IDeviceInfoService;
import com.txzt.gaea.business.device.service.IDevicePurchaseService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.enums.PurchaseStatus;
import org.beetl.sql.core.engine.PageQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device/purchase")
public class DevicePurchaseController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(DevicePurchaseController.class);

    @Autowired
    private IDevicePurchaseService devicePurchaseService;
    @Autowired
    private IDeviceInfoService deviceInfoService;

    /**
     * 分页查询
     * @param pageNumber
     * @param pageSize
     * @param devicePurchase
     * @return
     */
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize, DevicePurchase devicePurchase) {
        logger.info(devicePurchase.toString());
        PageQuery<DevicePurchase> pageList = devicePurchaseService.pageQuery(new PageQuery<>(pageNumber, pageSize, devicePurchase));
        pageList.getList().forEach(item -> {
            //翻译采购部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 新增
     * @param devicePurchase
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody DevicePurchase devicePurchase) {
        DeviceInfo deviceInfo = deviceInfoService.selectById(devicePurchase.getDeviceId());
        devicePurchase.setDeviceCode(deviceInfo.getDeviceCode());
        devicePurchase.setDeviceName(deviceInfo.getDeviceName());
        devicePurchase.setDeviceBrand(deviceInfo.getDeviceBrand());
        devicePurchase.setDeviceModel(deviceInfo.getDeviceModel());
        devicePurchase.setDeviceType(deviceInfo.getDeviceType());
        devicePurchase.setDevicePrice(deviceInfo.getDevicePrice());
        devicePurchase.setPurchaseStatus(PurchaseStatus.RECORD.getCode());
        devicePurchase.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        int count = devicePurchaseService.insertTemplate(devicePurchase);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 编辑
     * @param devicePurchase
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody DevicePurchase devicePurchase) {
        DeviceInfo deviceInfo = deviceInfoService.selectById(devicePurchase.getDeviceId());
        devicePurchase.setDeviceCode(deviceInfo.getDeviceCode());
        devicePurchase.setDeviceName(deviceInfo.getDeviceName());
        devicePurchase.setDeviceBrand(deviceInfo.getDeviceBrand());
        devicePurchase.setDeviceModel(deviceInfo.getDeviceModel());
        devicePurchase.setDeviceType(deviceInfo.getDeviceType());
        devicePurchase.setDevicePrice(deviceInfo.getDevicePrice());
        int count = devicePurchaseService.updateById(devicePurchase);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 提交
     * @param id
     * @return
     */
    @GetMapping(value = "/submit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> submit(@RequestParam Long id) {
        DevicePurchase devicePurchase = new DevicePurchase();
        devicePurchase.setId(id);
        devicePurchase.setPurchaseStatus(PurchaseStatus.PURCHASING.getCode());
        int count = devicePurchaseService.updateById(devicePurchase);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 完成
     * @param id
     * @return
     */
    @GetMapping(value = "/finish")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> finish(@RequestParam Long id) {
        int count = devicePurchaseService.finishDevicePurchase(id);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        DevicePurchase devicePurchase = new DevicePurchase();
        devicePurchase.setId(id);
        devicePurchase.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = devicePurchaseService.updateById(devicePurchase);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

}
